/*
 * ProductResource.cpp
 *
 *  Created on: 2013. 9. 11.
 *      Author: mega@gamevil.com
 */

#include "../User.h"
#include "../NUtil/Utils.h"
#include "../NResource/Static.h"
#include "ProductResource.h"

namespace ProjectZ { namespace NResource {

ProductResource::ProductResource()
{
	pProductAD	= new ProductData;
	pProductIOS	= new ProductData;
}

ProductResource::~ProductResource()
{
	delete pProductAD;
	delete pProductIOS;
}

bool
ProductResource::LoadResource()
{
	if (false == pProductAD->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pProductAD->LoadEventResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pProductIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	if (false == pProductIOS->LoadEventResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

ProductData::Flyweight *
ProductResource::GetFly(User * pUser, int tid)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pProductAD->GetFly(pUser, tid);

	case NUtil::MarketType::IOS_KAKAO:
		return pProductIOS->GetFly(pUser, tid);

	default:
		return NULL;
	}

	return NULL;
}

ProductData *
ProductResource::GetDataPtr(User * pUser)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(pUser->GetCompany(), pUser->GetSaleCode());

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pProductAD;

	case NUtil::MarketType::IOS_KAKAO:
		return pProductIOS;

	default:
		return NULL;
	}

	return NULL;
}

ProductData::~ProductData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<PRODUCTMAP::value_type>);
	std::for_each(clsFlyEventTree.begin(), clsFlyEventTree.end(), ProjectZ::NUtil::del_second_ptr<PRODUCTMAP::value_type>);
}

void
ProductData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<PRODUCTMAP::value_type>);
	clsFlyTree.clear();
}

void
ProductData::ClearEvent()
{
	std::for_each(clsFlyEventTree.begin(), clsFlyEventTree.end(), ProjectZ::NUtil::del_second_ptr<PRODUCTMAP::value_type>);
	clsFlyEventTree.clear();
}

bool
ProductData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_product WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		int seq;
		int tmpCompany, tmpSaleCode;

		ProductData::Flyweight * pFly = new ProductData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmpCompany >> tmpSaleCode
				>> pFly->_ITEM_NAME >> pFly->_ICON >> pFly->_TID >> pFly->_PRODUCT_KEY
				>> pFly->_POSITION >> pFly->_PRICE >> pFly->_VC_INDEX >> pFly->_GIFT_VC_INDEX
				>> pFly->_DEFAULT_AMOUNT >> pFly->_BONUS_AMOUNT >> pFly->_BONUS_RATE
				>> pFly->_DISCOUNT >> pFly->_IMG_URL >> pFly->_EXPLAIN;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[pFly->_TID] = pFly;
	}

	return true;
}

bool
ProductData::LoadEventResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery("SELECT * FROM resource_product_event WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	ClearEvent();

	while( clsAuto->Next() )
	{
		int seq;
		int tmpCompany, tmpSaleCode;

		::Toolkit::Str<3> NATIONAL_CODE;
		ProductData::Flyweight * pFly = new ProductData::Flyweight;

		clsAuto->GetRecord() >> NATIONAL_CODE >> seq >> tmpCompany >> tmpSaleCode
				>> pFly->_ITEM_NAME >> pFly->_ICON >> pFly->_TID >> pFly->_PRODUCT_KEY
				>> pFly->_POSITION >> pFly->_PRICE >> pFly->_VC_INDEX >> pFly->_GIFT_VC_INDEX
				>> pFly->_DEFAULT_AMOUNT >> pFly->_BONUS_AMOUNT >> pFly->_BONUS_RATE
				>> pFly->_DISCOUNT >> pFly->_IMG_URL >> pFly->_EXPLAIN;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyEventTree[pFly->_TID] = pFly;
	}

	return true;
}


ProductData::Flyweight *
ProductData::GetFly(User * pUser, int tid)
{
//	if (true == pUser->GetZENEventFlag())
	if (true == Static::InstancePtr()->GetEventTime()->IsZENDiscountEvent(pUser))
	{
		auto iter = clsFlyEventTree.find(tid);

		if (iter == clsFlyEventTree.end())
			return NULL;

		return iter->second;
	}
	else
	{
		auto iter = clsFlyTree.find(tid);

		if (iter == clsFlyTree.end())
			return NULL;

		return iter->second;
	}
}

ProductData::PRODUCTMAP &
ProductData::GetTree(User * pUser)
{
//	if (true == pUser->GetZENEventFlag())
	if (true == Static::InstancePtr()->GetEventTime()->IsZENDiscountEvent(pUser))
		return clsFlyEventTree;

	return clsFlyTree;
}

} /* NResource */ } /* ProjectZ */


